package com.me.web.system.controller;

import java.util.Date;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.me.common.annotation.Log;
import com.me.common.constant.UserConstants;
import com.me.common.core.controller.BaseController;
import com.me.common.core.domain.AjaxResult;
import com.me.common.core.domain.entity.SysRole;
import com.me.common.core.page.TableDataInfo;
import com.me.common.enums.BusinessType;
import com.me.common.utils.SecurityUtils;
import com.me.common.utils.StringUtils;
import com.me.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.me.web.system.domain.SysGroup;
import com.me.web.system.service.SysGroupService;


/**
 * 用户组管理Controller
 *
 * @author me
 * @date 2024-06-22
 */
@RestController
@RequestMapping("/system/group")
public class SysGroupController extends BaseController {
    @Autowired
    private SysGroupService sysGroupService;

    /**
     * 查询用户组管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:group:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysGroup sysGroup) {
        startPage();
        List<SysGroup> list = sysGroupService.queryList(sysGroup);
        return getDataTable(list);
    }

    /**
     * 导出用户组管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:group:export')")
    @Log(title = "用户组管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysGroup sysGroup) {
        List<SysGroup> list = sysGroupService.queryList(sysGroup);
        ExcelUtil<SysGroup> util = new ExcelUtil<SysGroup>(SysGroup.class);
        util.exportExcel(response, list, "用户组管理数据");
    }

    /**
     * 获取用户组管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:group:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(sysGroupService.get(id));
    }

    /**
     * 新增用户组管理
     */
    @PreAuthorize("@ss.hasPermi('system:group:add')")
    @Log(title = "用户组管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysGroup sysGroup) {
        return toAjax(sysGroupService.insert(sysGroup));
    }

    /**
     * 修改用户组管理
     */
    @PreAuthorize("@ss.hasPermi('system:group:edit')")
    @Log(title = "用户组管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysGroup sysGroup) {
        return toAjax(sysGroupService.update(sysGroup));
    }

    /**
     * 删除用户组管理
     */
    @PreAuthorize("@ss.hasPermi('system:group:remove')")
    @Log(title = "用户组管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(sysGroupService.delete(ids));
    }


    /**
     * 修改用户组状态
     * @param groupId
     * @param state
     * @return
     */
    @PreAuthorize("@ss.hasPermi('system:group:edit')")
    @PostMapping("/changeStatus/{groupId}/{state}")
    public AjaxResult changeStatus(@PathVariable("groupId") Long groupId,
                                   @PathVariable("state") String state) {
        boolean update = sysGroupService.update(new LambdaUpdateWrapper<SysGroup>()
                .eq(SysGroup::getId, groupId)
                .set(SysGroup::getStatus, state)
                .set(SysGroup::getUpdateBy, SecurityUtils.getNickName())
                .set(SysGroup::getUpdateTime, new Date()));
        return toAjax(update);
    }

    /**
     * 获取所有的角色
     */
    @PreAuthorize("@ss.hasPermi('system:group:query')")
    @GetMapping(value = "/getAllRoles")
    public AjaxResult getAllRoles() {
        return success(sysGroupService.getAllRoles());
    }
}
